package org.snail.chat.service.chatchain.quartz;

import lombok.extern.log4j.Log4j;
import org.snail.chat.service.impl.ChatServiceImpl;
import org.snail.common.dingding.utils.JedisUtils;
import org.snail.constant.CacheNameConstant;
import org.snail.department.service.impl.DepartmentTreeNodeService;
import org.snail.dept.bean.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Component
@Log4j
public class DingGroupSynQuartzJob {

    @Autowired
    private JedisUtils jedisUtils;

    @Autowired
    private ChatServiceImpl chatService;

    @Autowired
    private DepartmentTreeNodeService departmentTreeNodeService;

    //TODO 自动配置 项目加载就可以启动
    public void execute() {

        //里面自行写需要定时器执行的业务逻辑
        log.error("++++++++++++++++++++++此处执行触发器任务");
        try {
            //同步后，根据数据库重新生成部门树
            TreeNode treeNode = departmentTreeNodeService.getDepartmentTreeFromDB();     //获取部门树，参数为公司最上层的信息

            if (null == treeNode) {
                log.error("treeNode生成存在问题!treeNode为null");
            } else {
                departmentTreeNodeService.delTreeNode();
                jedisUtils.saveValue(CacheNameConstant.TREENODE_CACHE, treeNode);    //存入缓存中
            }

            //同步群组信息
            chatService.sychronizeChat(treeNode);
        } catch (Exception e) {
            log.error("定时器任务出错！详细错误请看错误信息及Log日志", e);
        }
        log.info("++++++++++++++++++++++此处执行触发器任务");
    }

}
